﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace SObject
{
    public partial class Object
    {
        public static class NameUtility
        {
            public static class Constant
            {
                public const char HierarchicalNameSeparator = ':';
                public const string FilenameExtension = "object.xml";

            }

            /// <summary>
            /// Retrieves the filename given the object name.
            /// Essentially, append the file extension.
            /// </summary>
            /// <param name="ObjectName"></param>
            /// <returns></returns>
            public static string GetFilename(string ObjectName)
            {
                string filename = ObjectName + "." + Constant.FilenameExtension;
                return (filename);
            }

            /// <summary>
            /// Retrieves the object name given the filename.
            /// Note, only the name before the extension will be return.
            /// For example, if filename is "NameWith.Dot." + FilenameExtension, then "Dot"
            /// will be returned.
            /// </summary>
            /// <param name="Filename"></param>
            /// <returns>Object's name</returns>
            public static string GetName(string Filename)
            {
                string name = "";
                // \w is the equivalent of [A-Za-z0-9_]
                string pattern = @"[\w-\.]+" + @"\." + Constant.FilenameExtension;
                Match match = Regex.Match(Filename, pattern);

                // If there was a match
                if (true == match.Success)
                {
                    //remove the extension
                    name = match.Value.Replace("." + Constant.FilenameExtension, "");
                }
                else
                {
                    StringBuilder stringBuilder = new StringBuilder();
                    stringBuilder.AppendFormat(@"The filename format ""{0}"" is not recognized.", Filename);
                    throw (new SObject.Exception(stringBuilder.ToString()));
                }

                if (false == IsNameValid(name))
                {
                    StringBuilder stringBuilder = new StringBuilder();
                    stringBuilder.AppendFormat(@"The resultant name ""{0}"" is not valid.", name);
                    throw (new SObject.Exception(stringBuilder.ToString()));
                }

                return (name);
            }

            public static bool IsNameValid(string Name)
            {
                bool valid = true;
                if (true == Name.Contains(Constant.HierarchicalNameSeparator))
                {
                    valid = false;
                }

                return (valid);
            }

            public static string ConstructCompleteName(string ParentCompleteName, string Name)
            {
                return (ParentCompleteName + Constant.HierarchicalNameSeparator + Name);
            }
        }
    }
}
